package com.bdsy.business.controller;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.bdsy.business.model.AppUpdate;
import com.bdsy.business.service.AppUpdateService;
import com.bdsy.common.utils.Result;
import com.bdsy.common.utils.UpDownUtil;
import com.bdsy.system.model.User;

@Controller
@RequestMapping(value = {"${web.adminPath}", "${web.appPath}"})
public class AppUpdateController {

	@Autowired
	AppUpdateService appUpdateService;
	
	
	@RequestMapping(value = "app")
	public String app() {
		return "admin/business/app";
	}
	
	/**
	 * 
	    * @Title: uploadApp
	    * @Description: 上传APP
	 */
	@RequestMapping("/uploadApp")
	@ResponseBody
	public Object uploadApp(MultipartFile file,AppUpdate appUpdate,HttpServletRequest request) {
		
			//存放路径
			String path = "/usr/local/tomcat/webapps/app";
//			String path = "E:\\Program Files\\Apache Software Foundation\\apache-tomcat-8.5.23\\webapps\\app";
		
			// 根据版本号查询是否存在 
			List<AppUpdate> app = appUpdateService.selectAppUpdateByVersion(appUpdate.getAppVersion());
			if(app == null || app.size() == 0) {
				// 返回上传成功之后 文件名
				String fileName = UpDownUtil.upload(file, path, appUpdate.getAppVersion());
				//把文件名存到数据库
				appUpdate.setAppUrl(fileName);
				User user = (User) request.getSession().getAttribute("user");
				appUpdate.setAppUserName(user.getUserName());
				boolean result = appUpdateService.uploadApp(appUpdate);
				if (result) {
					return Result.ok("上传成功");
				} else {
					return Result.error("上传失败");
				}
			} else {
				return Result.error("当前版本"+ appUpdate.getAppVersion() +"已存在");
			}
			
		}
	
	
	/**
	 * 
	    * @Title: checkUpdate
	    * @Description: 检查版本更新
	 */
	@RequestMapping("/checkUpdate")
	@ResponseBody
	public Object checkUpdate(String appVersion) {
		AppUpdate appUpdate = appUpdateService.selectAppUpdateByIsUpdate(1);
		// 获取服务器最新的版本号
		String newAppVersion = appUpdate.getAppVersion();
		Map<String,Object> map = new LinkedHashMap<>();

		if(!appVersion.equals(newAppVersion)) {
			map.put("code", 200);
			map.put("msg", "success");
			map.put("status", 1); //  提示更新
		}else {
			map.put("code", 200);
			map.put("msg", "success");
			map.put("status", 0); 
		}
		return map;
	}
	
	/**
	 * 
	    * @Title: downloadApp
	    * @Description:下载 App
	 */
	@RequestMapping("/downloadApp")
	@ResponseBody
	public Object downloadApp(HttpServletRequest request,HttpServletResponse response) {
		
		//获取下载的路径
		String filePath = "/usr/local/tomcat/webapps/app";
//		String filePath = "E:\\Program Files\\Apache Software Foundation\\apache-tomcat-8.5.23\\webapps\\app";
		// 获取下载的文件名
		AppUpdate appUpdate = appUpdateService.selectAppUpdateByIsUpdate(1);
		String fileName = appUpdate.getAppUrl();
		try {
			 UpDownUtil.download(request, response, fileName, filePath);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Map<String,Object> map = new LinkedHashMap<>();
		map.put("code", 200);
		map.put("msg", "success");
		return map;
	}
	
}
